#include "Timeline.h"

Timeline::Timeline(void)
{
	nextEvent = NULL;
}

Timeline::~Timeline(void)
{
	observers.clear();
}

void Timeline::LoadFromFile(const char *filename)
{
	std::ifstream file(filename);

	while (!file.eof())
	{
		TimelineEvent *tlEvent = new TimelineEvent();
		file >> tlEvent ->time;
		file >> tlEvent ->type;
		file >> tlEvent ->value;

		events.push(tlEvent);
	}

	file.close();
}

void Timeline::RegisterObserver(ITimelineObserver *observer)
{
	observers.push_back(observer);
}

void Timeline::UnregisterObserver(ITimelineObserver *observer)
{
	//observers.fin
}

void Timeline::ProcessTime(int time)
{
	if ((nextEvent == NULL) && !events.empty())
	{
		nextEvent = events.front();
		events.pop();
	}

	if ((nextEvent != NULL) && (nextEvent ->time <= time))
	{
		std::vector<ITimelineObserver*>::iterator i;
		for (i = observers.begin(); i != observers.end(); i++)
			(*i) ->Notify((float)nextEvent ->time, nextEvent ->type.c_str(), nextEvent ->value.c_str());

		nextEvent = NULL;
		ProcessTime(time);
	}
}
